Company = function() {
	return {
		initGrid : function() {
			$("#companyGrid").treegrid({
				url          : '/pc/company/tree.htm',
				nowrap       : false,
				rownumbers   : true,
				animate      : true,
				checkbox     : true,
				collapsible  : true,
				singleSelect : false,
				fit          : true,
				fitColumns   : true,
				idField      : 'company_id',
				treeField    : 'company_name',
				columns      : [[
				    { field : 'company_id',      title : '企业Id',     width : 50,    checkbox : true },
				    { field : 'company_name',    title : '企业名称',   width : 140 },
				    { field : 'company_code',    title : '企业编号',   width : 80 },
					{ field : 'company_address', title : '企业地址',   width : 140 },
					{ field : 'contact_man',     title : '联系人',     width : 80 },
					{ field : 'telephone',       title : '联系电话',   width : 80 },
					{ field : 'fax',             title : '传真',       width : 80 },
					{ field : 'deptNum',         title : '部门数(个)', width : 80,  align : 'center' },
					{ field : 'headshipNum',     title : '岗位数(个)', width : 80,  align : 'center' }
				]],
				toolbar     : [{
					text    : '添加',
					iconCls : 'add',
					handler : function() {
						Ict.openWin("新增企业", 450, 300, "/pc/company/add.htm");
					}
				}, {
					text    : '修改',
					iconCls : 'edit',
					handler : function() {
						var row = $('#companyGrid').treegrid('getSelections');
						if(1 == row.length) {
							Ict.openWin('修改企业信息', 450, 300, "/pc/company/edit.htm?companyId=" + row[0].company_id);
						} else if(0 == row.length) {
							Ict.info("请选择你要修改的企业!");
							return;
						} else {
							Ict.info("请选择一条记录进行修改!");
							return;
						}
					}
				}, {
					text    : '删除',
					iconCls : 'remove',
					handler : function(){
						var rows = $('#companyGrid').treegrid('getSelections');
						var companyIds = null;
						if (0 == rows.length) {
							Ict.info("请选择你要删除的企业!");
							return;
						}
						for(var i = 0; i < rows.length; i++) {
							if("Y" == rows[i].have_child_company) {
								Ict.info("请先删除子企业!");
								return;
							}
						}
						for(var i = 0; i < rows.length; i++) {
							if(rows[i].deptNum > 0) {
								Ict.info("请先删除企业下的部门!");
								return;
							}
						}
						for(var i = 0; i < rows.length; i++) {
							if(rows[i].headshipNum > 0) {
								Ict.info("请先删除企业下的岗位!");
								return;
							}
						}
						Ict.confirm('确定要删除选中的企业吗?', function(btn) {
							if(btn) {
								for(var i = 0; i < rows.length; i++) {
									if (null == companyIds || i == 0) {
										companyIds = rows[i].company_id;
									} else {
										companyIds = companyIds + "," + rows[i].company_id;
									}
								}
								$.ajax({
									url     : '/pc/company/delete.htm?companyIds=' + companyIds,
									type    : 'post',
									success : function(data) {
										if (data == "SUCCESS") {
											$('#companyGrid').treegrid('reload');
											Ict.info('操作成功!共删除' + rows.length + '条记录!');
										} else {
											Ict.error("删除失败!");
										}
									}
								});
							}
						});
					}
				}, {
					text    : '刷新',
					iconCls : 'reload',
					handler : function() {
						$('#companyGrid').treegrid('reload');
					}
				}, {
					text    : '全部展开',
					iconCls : 'expand',
					handler : function() {
						$('#companyGrid').treegrid('expandAll');
					}
				}, {
					text    : '全部收起',
					iconCls : 'collapse',
					handler : function() {
						$('#companyGrid').treegrid('collapseAll');
					}
				}]
			});
		},
		
		addSearchBox : function(width) {
			$('.searchbox').css({'width': width+'px'});
			$('.searchbox-text').css({'width': eval(width-20)+'px'});
			$('.datagrid-toolbar').append($('.serarchbox'));
			$('.datagrid-toolbar').append($('#toolbar'));
		},
		
		doSearch : function(value) {
			if("" == $.trim(value)) {
				Ict.info("请输入查询条件!");
				return;
			}
			$.ajax({
				url     : '/pc/company/search.htm',
				type    : 'POST',
				data    : {
					"companyName" : value
				},
				success : function(data) {
					$("#companyGrid").treegrid('unselectAll');
					for(var i = 0; i < data.length; i++) {
						$("#companyGrid").treegrid('select', data[i].company_id);
					}
				}
			});
		},
		
		photo : function(value) {
			value = "/pc/company/images/" + value;
			art.dialog({
				content   : '<img src=' + value + '>',
				width     : 'auto',
				height    : 'auto',
				zIndex    : "20000",
				left      : '40%',
			    top       : '30%',
			    lock      : true,
			    cancelVal : '关闭',
			    cancel    : true
			});
		},
		
		init : function() {
			Company.initGrid();
			Company.addSearchBox(250);
		}
	};
}();

$(function(){
	Company.init();
});